package org.juggers.component;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.juggers.domain.Jugger;
import org.juggers.domain.SendMailException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.velocity.VelocityEngineUtils;

/**
 * @author <a href="mailto:michel@breizhjug.org">Michel DAVID</a>
 */
@Component( "mailSender" )
public class DefaultMailSender
    implements MailSender
{

    private static Log logger = LogFactory.getLog( DefaultMailSender.class );

    private String sender;

    @Autowired
    private JavaMailSender mailService;

    @Autowired
    private VelocityEngine velocityEngine;

    @Override
    public void send( String template, String subject, Jugger jugger )
        throws SendMailException
    {
        send( template, subject, jugger, new HashMap<String, Object>() );
    }

    @Override
    public void send( String template, String subject, Jugger jugger, Map<String, Object> parameters )
        throws SendMailException
    {
        parameters.put( "jugger", jugger );
        parameters.put( "template" , template );

        MimeMessage mimeMessage = mailService.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper( mimeMessage );

        try
        {
            message.setFrom( sender );
            message.setTo( jugger.getEMail() );
            message.setSubject( subject );

            String mailBody =
                VelocityEngineUtils.mergeTemplateIntoString( velocityEngine, "velocity/main.vm", parameters );
            message.setText( mailBody, true );

            mailService.send( mimeMessage );
        }
        catch ( Exception ex )
        {
            logger.error( "Error sending mail", ex );
            throw new SendMailException();
        }

    }

    public void setSender( String sender )
    {
        this.sender = sender;
    }
}
